//用户表
const { User } = require("@models/user");
// 身份证表
const { IdCard } = require("@models/idcard");
// 报名表
const { Enroll } = require("@models/enroll");
// 活动表
const { Activity } = require("@models/activity");
// 用户地址表
const { Address } = require("@models/address");
// 奖品表
const { Raffle } = require("@models/raffle");
// 中奖记录表
const { Raffle_record } = require("@models/raffle");
// 商品表
const { Goods } = require("@models/goods");
// 定时任务表
const { Task } = require("@models/task");
const { TextMsg } = require("@models/textmsg");
// 新闻表
const { News } = require("@models/news");

const { UserPoint, PointRecord } = require("@models/point");

const { Order } = require("@models/order");
const { Admin } = require("@models/admin");
const { Rebate } = require("@models/rebate");
const { Favor } = require("@models/favor");
const { Reply, Complaint } = require("@models/complaint");
const { BlackList } = require("@models/blacklist");
// 建立表关联
User.hasMany(Enroll, { foreignKey: "uid", constraints: false });
Enroll.belongsTo(User, { foreignKey: "uid", constraints: false });

Activity.hasMany(Enroll, { foreignKey: "activity_id", constraints: false });
Enroll.belongsTo(Activity, { foreignKey: "activity_id", constraints: false });
Enroll.belongsTo(Admin, { foreignKey: "admin_id", constraints: false });

// 在 Project 模型上定义关联到 User 模型（没有外键约束）
Address.belongsTo(User, { foreignKey: "uid", constraints: false });

// Raffle_record.belongsTo(Address, {
//   foreignKey: "address_id",
//   constraints: false,
// });

Raffle_record.belongsTo(Raffle, {
  foreignKey: "raffle_id",
  constraints: false,
});

Raffle_record.belongsTo(User, {
  foreignKey: "uid",
  constraints: false,
});

Raffle_record.belongsTo(Admin, {
  foreignKey: "admin_id",
  constraints: false,
});

PointRecord.belongsTo(Admin, {
  foreignKey: "operator_id",
  targetKey: "admin_id",
  constraints: false,
});

Rebate.belongsTo(User, {
  foreignKey: "uid",
  constraints: false,
});

Order.belongsTo(User, {
  foreignKey: "uid",
  constraints: false,
});

UserPoint.belongsTo(Admin, {
  foreignKey: "operator_id",
  targetKey: "admin_id",
  constraints: false,
});

Task.belongsTo(Admin, {
  foreignKey: "admin_id",
  constraints: false,
});

TextMsg.belongsTo(Admin, {
  foreignKey: "admin_id",
  constraints: false,
});

Favor.belongsTo(User, {
  foreignKey: "uid",
  constraints: false,
});

Favor.belongsTo(News, {
  foreignKey: "news_id",
  constraints: false,
});

News.belongsTo(Admin, {
  foreignKey: "admin_id",
  constraints: false,
});

Complaint.hasMany(Reply, {
  foreignKey: "complaint_id",
  constraints: false,
});

Reply.belongsTo(Complaint, {
  foreignKey: "complaint_id",
  onDelete: "CASCADE", // 删除关联模型时级联删除
  constraints: false,
});

Complaint.belongsTo(User, {
  foreignKey: "uid",
  constraints: false,
});

User.hasOne(BlackList, {
  foreignKey: "uid",
  constraints: false,
});

Reply.belongsTo(Admin, {
  foreignKey: "admin_id",
  constraints: false,
});

Raffle_record.belongsTo(User, { foreignKey: "uid", constraints: false });

Order.belongsTo(Goods, { foreignKey: "goods_id", constraints: false });

// User.hasOne(IdCard, {
//   foreignKey: "eid",
//   constraints: false,
// });

User.belongsTo(IdCard, { foreignKey: "eid", constraints: false });
